<template>
    <!-- 三级分类模块 -->
    <Cagetory :scenne="scenne"></Cagetory>

    <el-card style="margin: 10px 0;">
        <div v-show="scenne == 0">
            <!-- 表格展示开始 -->
            <el-button @click="addSpu" icon="Plus" type="primary"
                :disabled="cagetoryStore.c3Id ? false : true">添加SPU</el-button>
            <!-- 进行场景切换 0 代表已有的数据 1 代表添加或者修改spu模块 2 代表sku -->
            <el-table border style="margin: 10px 0;" :data="records">
                <el-table-column label="序号" type="index" align='center' width="80px"></el-table-column>
                <el-table-column label="SPU名称" prop="spuName"></el-table-column>
                <el-table-column label="SPU描述" prop="description"></el-table-column>
                <el-table-column label="SPU操作">
                    <template #="{ row }">
                        <el-button icon="Plus" title="添加SKU" type="primary" @click="addSku(row)"></el-button>
                        <el-button icon="Edit" title="修改SPU" type="primary" @click="updateSpu(row)"></el-button>
                        <el-button icon="View" title="查看SKU列表" type="info" @click="findSku(row)"></el-button>
                        <el-popconfirm :title="`你确定删除${row.spuName}吗`" @confirm="deleteSpu(row)" width="200px">
                            <template #reference>
                                <el-button icon="Delete" title="删除SPU" type="warning"></el-button>
                            </template>
                        </el-popconfirm>

                    </template>
                </el-table-column>
            </el-table>
            <!-- 表格展示结束 -->
            <!-- 分页器开始 -->
            <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 9]"
                :background="true" layout="prev, pager, next, jumper,->,sizes,total" :total="total"
                @current-change="getHasSpu" @size-change="changeSize" />
            <!-- 分页器结束 -->
        </div>
        <SpuForm ref="spu" v-show="scenne == 1" @changeScene="changeScene"></SpuForm>
        <SkuForm ref="sku" v-show="scenne == 2" @changeScene="changeScene"></SkuForm>
        <!-- dialog对话框:展示已有的SKU数据 -->
        <el-dialog v-model="show" title="SKU列表" center>
            <el-table border :data="skuArr">
                <el-table-column label="SKU名字" prop="skuName"></el-table-column>
                <el-table-column label="SKU价格" prop="price"></el-table-column>
                <el-table-column label="SKU重量" prop="weight"></el-table-column>
                <el-table-column label="SKU图片">
                    <template #="{ row }">
                        <img :src="row.skuDefaultImg" style="width: 100px;height: 100px;">
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
    </el-card>


</template>

<script setup lang="ts">
import { ref, watch,onBeforeUnmount } from "vue"
import type { HasSpuResponseData, Records, SpuData, SkuData } from "@/api/product/spu/type"
import useCategoryStore from "@/store/modules/cagetory";
import { reqHasSpu, reqSkuList, reqRemoveSpu } from "@/api/product/spu";
import SkuForm from "./skuForm.vue"
import SpuForm from "./spuForm.vue"
import { ElMessage } from "element-plus";
let cagetoryStore = useCategoryStore()
let scenne = ref<Number>(0)
// 定义初始分页器当前页码
let pageNo = ref<number>(1)
// 分页器显示页码
let pageSize = ref<Number>(3)
// 定义接收已有数据的数组
let records = ref<Records>([])
// 定义数据总条数
let total = ref<Number>(0)
// 获取spuForm实例
let spu = ref<any>()
// 获取skuForm实例
let sku = ref<any>()

// 定义对话框显隐的变量
let show = ref<boolean>(false)
//存储全部的SKU数据
let skuArr = ref<SkuData[]>([]);
// 当三级分类id有值，获取已有的SPU数据
watch(() => cagetoryStore.c3Id, () => {
    // 当第三级分类id没有的话，不执行
    if (!cagetoryStore.c3Id) return
    getHasSpu()
})

// 获取spu数据的方法
const getHasSpu = async (pager = 1) => {
    pageNo.value = pager
    let result: HasSpuResponseData = await reqHasSpu(pageNo.value, pageSize.value, cagetoryStore.c3Id);
    if (result.code == 200) {
        records.value = result.data.records
        total.value = result.data.total
    }

}
// 分页器下拉框数据变化触发的回调
const changeSize = () => {
    getHasSpu()
}
// 添加spu按钮的回调
const addSpu = () => {
    scenne.value = 1
    // 触发子组件的方法
    spu.value.initAddSpu(cagetoryStore.c3Id)
}
// 子组件SpuForm绑定自定义事件:目前是让子组件通知父组件切换场景为0
const changeScene = (obj: any) => {
    scenne.value = obj.flag
    // 重新发起请求，再次获取数据
    // 判断此次请求时何种类型
    if (obj.params == "update") {
        // 更新留在当前页
        getHasSpu(pageNo.value)
    } else {
        // 添加留在第一页
        getHasSpu()
    }
}
// 点击编辑按钮的回调
const updateSpu = (row: SpuData) => {
    //切换为场景1:添加与修改已有SPU结构->SpuForm
    scenne.value = 1;
    //调用子组件实例方法获取完整已有的SPU的数据
    spu.value.initHasSpuData(row);
}

// 点击添加Sku按钮的回调
const addSku = (row: SpuData) => {
    // 切换场景为2
    scenne.value = 2;
    // 调用子组件的初始化sku方法
    sku.value.initSkuData(cagetoryStore.c1Id, cagetoryStore.c2Id, row);
}

// 点击查看SKU列表按钮的回调
const findSku = async (row: any) => {
    let result: any = await reqSkuList((row.id as number))
    if (result.code === 200) {
        skuArr.value = result.data
        //对话框显示出来
        show.value = true;
    }
}

// 点击删除spu按钮的回调
const deleteSpu = async (row: SpuData) => {
    let result = await reqRemoveSpu((row.id as number))
    if (result.code === 200) {
        ElMessage({
            type:'success',
            message:'删除成功'
        })
        // 刷新ui
        getHasSpu(records.value.length > 0 ? pageNo.value : pageNo.value - 1)

    }else{
        ElMessage({
            type:'error',
            message:'删除失败'
        })
    }    
}
//路由组件销毁前，情况仓库关于分类的数据
onBeforeUnmount(() => {
    cagetoryStore.$reset();
})
</script>
<style lang="scss" scoped></style>